package com.bookstore.mapper;

import com.bookstore.domain.Address;
import com.bookstore.domain.Order;
import com.bookstore.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface OrderMapper {
    //获取所有订单消息
    @Select("<script>" +
            "select o.*,u.username FROM b_order o,book_user u" +
            "<where>" +
            "o.uid=u.`uid`" +
            "<if test=\"uid!=0\"> and o.uid = #{uid}</if>" +
            "<if test=\"isPay!=-1\"> and isPay=#{isPay}</if>" +
            "<if test=\"isDeliver!=-1\"> and isDeliver=#{isDeliver}</if>" +
            "<if test=\"username!=null\"> and username like concat(\"%\",#{username},\"%\")</if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "orderId", column = "orderId"),
            @Result(property = "createTime", column = "createTime"),
            @Result(property = "uid",
                    javaType = User.class,
                    column = "uid",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "buyers",
                    javaType = User.class,
                    column = "buyers",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "totalPrice", column = "totalPrice"),
            @Result(property = "isPay", column = "isPay"),
            @Result(property = "addressId",
                    javaType = Address.class,
                    column = "addressId",
                    one = @One(select = "com.bookstore.mapper.AddressMapper.findByAddressId")
            ),
            @Result(property = "payTime", column = "payTime"),
            @Result(property = "deliverTime", column = "deliverTime"),
            @Result(property = "isDeliver", column = "isDeliver"),
            @Result(property = "isReceipt", column = "isReceipt"),
            @Result(property = "isRefund", column = "isRefund")

    })
    public List<Order> findAll(@Param("isPay") Integer isPay, @Param("isDeliver") Integer isDeliver, @Param("username")
            String username, @Param("uid") Integer uid);


    //根据uid获取订单信息///用于用户查询自己的订单
    @Select("<script>" +
            "select o.*,u.username FROM b_order o,book_user u" +
            "<where>" +
            "o.uid=u.`uid` and o.buyers = #{buyers}" +
            "<if test=\"isPay!=-1\"> and isPay=#{isPay}</if>" +
            "<if test=\"isDeliver!=-1\"> and isDeliver=#{isDeliver}</if>" +
            "<if test=\"username!=null\"> and username like concat(\"%\",#{username},\"%\")</if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "orderId", column = "orderId"),
            @Result(property = "createTime", column = "createTime"),
            @Result(property = "uid",
                    javaType = User.class,
                    column = "uid",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "buyers",
                    javaType = User.class,
                    column = "buyers",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "totalPrice", column = "totalPrice"),
            @Result(property = "isPay", column = "isPay"),
            @Result(property = "addressId",
                    javaType = Address.class,
                    column = "addressId",
                    one = @One(select = "com.bookstore.mapper.AddressMapper.findByAddressId")
            ),
            @Result(property = "payTime", column = "payTime"),
            @Result(property = "deliverTime", column = "deliverTime"),
            @Result(property = "isDeliver", column = "isDeliver"),
            @Result(property = "isReceipt", column = "isReceipt"),
            @Result(property = "isRefund", column = "isRefund")

    })
    public List<Order> findByUid(@Param("isPay") Integer isPay, @Param("isDeliver") Integer isDeliver,
                                 @Param("buyers") Integer buyers, @Param("username") String username);

    //添加订单信息
//    @Insert("insert into b_order values()")
    public Integer addOrder(Order order);

    //确认用户已付款才能发货
    @Update("update b_order set isDeliver=#{isDeliver},deliverTime=now() where isPay = 1 and orderId=#{orderId}")
    Integer upDeliver(@Param("orderId") String orderId, @Param("isDeliver") Integer isDeliver);

    //处理退款
    @Update("update b_order set isRefund = #{refund} where orderId=#{orderId}")
    int upRefund(@Param("orderId") String orderId, @Param("refund") int refund);

    //付款
    @Update("update b_order set isPay=1 ,payTime=now() where orderId=#{orderId}")
    int upPay(@Param("orderId") String orderId);

    //收货
    @Update("update b_order set isReceipt = 1 where orderId=#{orderId}")
    int upReceipt(String orderId);

    //申请退货
    @Update("update b_order set isRefund = 1 where orderId=#{orderId}")
    int userUpRefund(String orderId);

    @Select("select * from b_order o,address a where orderId=#{orderId} AND a.addressId = o.addressId")
    @Results({
            @Result(property = "addressId",
                    javaType = Address.class,
                    column = "addressId",
                    one = @One(select="com.bookstore.mapper.AddressMapper.findByAddressId")
            )
    })
    Order findOneByOrderId(@Param("orderId") String orderId);

    @Update("update b_order set addressId=#{addressId.addressId} where orderId=#{orderId}")
    int upOrder(Order order);

    @Delete("delete from b_order where orderId=#{orderId} and buyers=#{buyers}")
    int delOrderByOrderIdAndBuyers(@Param("orderId") String orderId, @Param("buyers")Integer buyersId);
    @Delete("delete from b_order_details where orderId=#{orderId}")
    int delOrderDetailsByOrderId(@Param("orderId") String orderId);
    @Insert("Insert into b_order(orderId,uid,buyers,totalPrice,isPay,addressId,isDeliver,isReceipt,isRefund,payTime)" +
            " values(#{orderId},#{uid.uid},#{buyers.uid},#{totalPrice},#{isPay},#{addressId.addressId},#{isDeliver},#{isReceipt},#{isRefund},now())")
    int Pay(Order o);
}
